package org.dromara.xyyw.controller;

import java.util.List;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.xyyw.domain.bo.XyywCouponUsageRecordBo;
import org.dromara.xyyw.domain.query.XyywCouponUsageRecordQuery;
import org.dromara.xyyw.domain.vo.XyywCouponUsageRecordVo;
import org.dromara.xyyw.service.IXyywCouponUsageRecordService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 优惠券使用记录
 *
 * @author yf
 * @date 2024-05-29
 */
@Validated
@RestController
@RequestMapping("/xyyw/couponUsageRecord")
public class XyywCouponUsageRecordController extends BaseController {

    @Autowired
    private IXyywCouponUsageRecordService xyywCouponUsageRecordService;

    /**
     * 查询优惠券使用记录列表
     */
    @SaCheckPermission("xyyw:couponUsageRecord:list")
    @GetMapping("/list")
    public TableDataInfo<XyywCouponUsageRecordVo> list(XyywCouponUsageRecordQuery query) {
        return xyywCouponUsageRecordService.queryPageList(query);
    }

    /**
     * 导出优惠券使用记录列表
     */
    @SaCheckPermission("xyyw:couponUsageRecord:export")
    @Log(title = "优惠券使用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XyywCouponUsageRecordQuery query, HttpServletResponse response) {
        List<XyywCouponUsageRecordVo> list = xyywCouponUsageRecordService.queryList(query);
        ExcelUtil.exportExcel(list, "优惠券使用记录", XyywCouponUsageRecordVo.class, response);
    }

    /**
     * 获取优惠券使用记录详细信息
     *
     * @param usageId 主键
     */
    @SaCheckPermission(value = {"xyyw:couponUsageRecord:query", "xyyw:couponUsageRecord:edit"}, mode = SaMode.OR)
    @GetMapping("/{usageId}")
    public R<XyywCouponUsageRecordVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer usageId) {
        return R.ok(xyywCouponUsageRecordService.queryById(usageId));
    }

    /**
     * 新增优惠券使用记录
     */
    @SaCheckPermission("xyyw:couponUsageRecord:add")
    @Log(title = "优惠券使用记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XyywCouponUsageRecordBo bo) {
        return toAjax(xyywCouponUsageRecordService.insertByBo(bo));
    }

    /**
     * 修改优惠券使用记录
     */
    @SaCheckPermission("xyyw:couponUsageRecord:edit")
    @Log(title = "优惠券使用记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XyywCouponUsageRecordBo bo) {
        return toAjax(xyywCouponUsageRecordService.updateByBo(bo));
    }

    /**
     * 删除优惠券使用记录
     *
     * @param usageIds 主键串
     */
    @SaCheckPermission("xyyw:couponUsageRecord:remove")
    @Log(title = "优惠券使用记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{usageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Integer[] usageIds) {
        return toAjax(xyywCouponUsageRecordService.deleteWithValidByIds(List.of(usageIds)));
    }
}
